package com.donglan.pethome.basic.service.impl;

import com.donglan.pethome.basic.mapper.BaseMapper;
import com.donglan.pethome.basic.query.BaseQuery;
import com.donglan.pethome.basic.service.BaseService;
import com.donglan.pethome.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: zhangdonghui
 * @Date: 2022/1/8 11:36
 */
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements BaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    @Transactional
    public void add(T t) {
        baseMapper.add(t);
    }

    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        baseMapper.delete(id);
    }

    @Override
    public T loadById(Long id) {
        return baseMapper.loadById(id);
    }

    @Override
    public List<T> loadAll() {
        return baseMapper.loadAll();
    }

    @Override
    public PageList<T> loadPageByQuery(BaseQuery query) {
        Long count = baseMapper.loadCountByQuery(query);
        if(count.equals(0L)){
            return new PageList<T>();
        }
        List<T> result = baseMapper.loadByQuery(query);
        return new PageList<>(count,result);
    }

    @Override
    @Transactional
    public void batchRemove(List<T> ts) {
        baseMapper.batchRemove(ts);
    }
}
